home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
58189
/
58189.xpi
/
modules
/
Whitelist.jsm
< prev
Wrap
Text File
|
2010-01-07
|
6KB
|
222 lines
/*
* This class implements a whitelist for domains. This means that certain
* domains can be freed from policy decisions.
* ATTENTION: initialization is done by loadList(), but can not be done at
* startup time. Apparantly, Firefox does not find the preferences and thinks
* they are not set. Only after loading the browser, the prefs are available.
*/
var EXPORTED_SYMBOLS = [ ];
Components.utils.import("resource://csfiremodules/CsFireCommon.jsm");
CsFire.nsJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON);
CsFire.Whitelist = new function() {
this.list = [];
this.loaded = false;
};
/*
* Checks whether there are whitelist entries matching the current request and
* returns either the whitelist decision (accept) or null if no match was found
*/
CsFire.Whitelist.decide = function(data) {
var src = data.referrer_host;
var dst = data.dst_host;
var result = false;
var list = this.getList(); //Loads the list if this has not been done yet
for(var i in list) {
var item = list[i];
if(item) {
if(item.enabled && this.isMatch(dst, item.destination) && this.isMatch(src, item.origin)) {
return { "action": CsFire.Policy.ACCEPT,
"whitelist": true };
}
}
}
return null;
}
/*
* Checks whether a certain URI matches agains a certain domain
*/
CsFire.Whitelist.isMatch = function(stringUri, domain) {
var result = true;
var splitUri;
stringUri == null ? splitUri = ["null"] : splitUri = stringUri.split(".");
var splitDomain = domain.split(".");
var indexUri = splitUri.length - 1;
var indexDomain = splitDomain.length - 1;
while(indexUri >= 0 && indexDomain >= 0 && result) {
if(splitDomain[indexDomain] != "*" && (splitDomain[indexDomain] != splitUri[indexUri])) {
result = false;
}
indexUri--;
indexDomain--;
}
//Check if the match has been completed
//(e.g. domain=test.www.google.be and uri=www.google.be is not a match!)
if(result && indexDomain >= 0) {
result = false;
}
return result;
}
/*
* Adds an entry to the whitelist using the provided origin and destination. If
* one of these is invalid, null is returned. Otherwise, the added rule is
* returned as an object.
*/
CsFire.Whitelist.addEntry = function(origin, destination) {
var result = null;
try {
if(this.isValidDomain(origin) && this.isValidDomain(destination)) {
var theList = this.getList();
var exists = false;
for(var i = 0; i < theList.length && !exists; i++) {
var item = theList[i];
if(item && item.origin == origin && item.destination == destination) {
exists = true;
}
}
if(!exists) {
var item = {"origin": origin,
"destination": destination,
"enabled": true};
// Add item to end of list
this.list.push(item);
// Make the change persistent
this.storeList();
result = item;
}
}
else {
result = { "error": "Invalid input" };
if(!this.isValidDomain(origin)) {
result.errororigin = true;
}
if(!this.isValidDomain(destination)) {
result.errordestination = true;
}
}
}
catch(e) {
CsFire.Logger.error("Failed to add domain to whitelist: " + e);
result = { "error": "Error adding domain" };
}
return result;
};
/*
* Changes the "enabled" status of an entry
*/
CsFire.Whitelist.updateEntryStatus = function(entry) {
var result = null;
var done = false;
for(var i = 0; i < this.list.length && !done; i++) {
var item = this.list[i];
if(item && item.origin == entry.origin && item.destination == entry.destination) {
CsFire.Logger.debug("Changing state of item to " + (!item.enabled));
item.enabled = !(item.enabled);
done = true;
}
}
this.storeList();
};
/*
* Removes an entry from the whitelist
*/
CsFire.Whitelist.removeEntry = function(origin, destination) {
var result = true;
try {
/*
* Simply set the index to null. The item will be removed next time the
* browser is loaded and the list is retrieved from the prefs.
*/
var done = false;
for(var i = 0; i < this.list.length && !done; i++) {
var item = this.list[i];
if(item && item.origin == origin && item.destination == destination) {
delete this.list[i];
done = true;
}
}
// Make the change persistent
this.storeList();
}
catch(e) {
CsFire.Logger.error("Failed to remove domain from whitelist: " + e);
result = false;
}
return result;
};
/*
* Returns a list of entries in the whitelist. Loads the list if this has not
* been done yet.
*/
CsFire.Whitelist.getList = function() {
// Load the list if this has not been done before
this.loadList();
return this.list;
}
/*
* Checks whether the provided domain is a valid domain name
*/
CsFire.Whitelist.isValidDomain = function(domain) {
var result = true;
var splitted = domain.split(".");
for(var i = 0; i < splitted.length && result; i++) {
var item = splitted[i];
if(item == null || item == "" || (item != "*" && !item.match(/^[a-zA-Z0-9\-_]+$/))) {
result = false;
}
}
return result;
};
/*
* Loads the list from the preferences into memory
*/
CsFire.Whitelist.loadList = function() {
// If loaded before, abort
if(this.loaded) return;
this.loaded = true;
var s = CsFire.PreferenceUtils.getStringPreference("extensions.csfire.whitelist", null);
if(s) {
var value = CsFire.nsJSON.decode(s);
for(var i in value) {
var entry = value[i];
if(entry != null) {
this.list.push(entry);
}
}
}
};
/*
* Stores the list from memory to preferences.
*/
CsFire.Whitelist.storeList = function() {
var storeList = this.getList();
var value = CsFire.nsJSON.encode(storeList);
CsFire.PreferenceUtils.setStringPreference("extensions.csfire.whitelist", value);
};